home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 70 / IOPROG_70.ISO / soft / Codice / Libro Allegato / progAvanzataWord.bas < prev    next >
Encoding:
BASIC Source File  |  2003-05-02  |  11.1 KB  |  348 lines

  1. Attribute VB_Name = "progAvanzataWord"
  2. Sub prendiLinks()
  3. '---------------------------------------------------
  4. ' Visualizza gli elementi dell'insieme Hyperlinks
  5. ' del documento attivo e chiede al'utente se seguirli
  6. ' (applicando il metodo Follow) o meno
  7. '---------------------------------------------------
  8. ' (c) 2003 Ivan Venuti e Marinella Lizza
  9. '---------------------------------------------------
  10.  
  11.  Dim links As Word.Hyperlinks
  12.  Set links = ActiveDocument.Hyperlinks
  13.  For Each elemento In links
  14.     With elemento
  15.     risp = MsgBox( _
  16.         "Address =" & vbTab & .Address & vbLf & _
  17.         "TextToDisplay =" & vbTab & .TextToDisplay & vbLf & _
  18.         "SubAddress =" & vbTab & .SubAddress & vbLf & _
  19.         "Target =" & vbTab & vbTab & .Target & vbLf & _
  20.         "Range =" & vbTab & vbTab & .Range & vbLf & VBA.vbLf & _
  21.         "Eseguire il metodo follow?", vbYesNo, _
  22.         "Link del documento")
  23.     If risp = VBA.vbYes Then elemento.Follow
  24.     End With
  25.  Next
  26. End Sub
  27.  
  28. Function inserisci(vettore() As String, elemento As String, max As Integer) As Integer
  29. '---------------------------------------------------
  30. ' Scorre il vettore, che si suppone ordinato,
  31. ' e inserisce il nuovo elemento mantenendo
  32. ' l'ordinamento lessicografico
  33. '---------------------------------------------------
  34. ' (c) 2003 Ivan Venuti e Marinella Lizza
  35. '---------------------------------------------------
  36.     MsgBox "Inserisci " & elemento
  37.     indice = 0
  38.     While indice < max And vettore(indice) < elemento
  39.         indice = indice + 1
  40.     Wend
  41.     If indice = max Or vettore(indice) > elemento Then
  42.         inserisci = 1
  43.         For i = max To indice Step -1
  44.             vettore(i + 1) = vettore(i)
  45.         Next
  46.         vettore(indice) = elemento
  47.     Else
  48.         inserisci = 0
  49.     End If
  50. End Function
  51.  
  52. Function collectEmailAddresses() As String()
  53. '---------------------------------------------------
  54. ' Prende tutti gli indirizzi email dall'insieme
  55. ' Hyperlinks del documento attivo e li memorizza
  56. ' in ordine lessicografico in un vettore, eliminando
  57. ' eventuali indirizzi uguali.
  58. ' Restituisce il vettore.
  59. '---------------------------------------------------
  60. ' (c) 2003 Ivan Venuti e Marinella Lizza
  61. '---------------------------------------------------
  62.     Dim indirizzi() As String
  63.     Dim indiceMax As Integer
  64.     Dim indice As Integer
  65.     indiceMax = 1
  66.     indice = 0
  67.     ReDim indirizzi(indiceMax)
  68.     ' reperisce solo gli indirizzi di email
  69.     For Each elemento In ActiveDocument.Hyperlinks
  70.         If (VBA.Left(elemento.Address, 7) = "mailto:") Then
  71.             ' Φ prorpio un indirizzo di email
  72.             incremento = inserisci(indirizzi, _
  73.                 VBA.Mid(elemento.Address, 8), indice)
  74.             indice = indice + incremento
  75.             If (indice >= indiceMax) Then
  76.                 ' E' necessario ridimensionare il vettore
  77.                 indiceMax = indiceMax * 2
  78.                 ReDim Preserve indirizzi(indiceMax)
  79.             End If
  80.         End If
  81.     Next
  82.     collectEmailAddresses = indirizzi
  83. End Function
  84.  
  85.  
  86.  
  87. Sub folderEmailAddresses()
  88. ' Crea un nuovo folder di email i cui indirizzi sono
  89. ' gli indirizzi email contenuti nel documento attivo
  90. '---------------------------------------------------
  91. ' (c) 2003 Ivan Venuti e Marinella Lizza
  92. '---------------------------------------------------
  93.  
  94.     Dim applic As Outlook.Application
  95.     Set applic = New Outlook.Application
  96.     Dim folder As Outlook.MAPIFolder
  97.     Dim space As Outlook.NameSpace
  98.     Set space = applic.GetNamescape("MAPI")
  99.     
  100.     Dim indirizzi() As String
  101.     indirizzi = collectEmailAddresses()
  102. End Sub
  103.  
  104.  
  105.  
  106.  
  107. Sub sendEmailAddresses()
  108. '---------------------------------------------------
  109. ' Crea un nuovo folder di email i cui indirizzi sono
  110. ' gli indirizzi email contenuti nel documento attivo
  111. '---------------------------------------------------
  112. ' (c) 2003 Ivan Venuti e Marinella Lizza
  113. '---------------------------------------------------
  114.  
  115.     Dim applic As Outlook.Application
  116.     Dim messaggio As Outlook.MailItem
  117.     Set applic = New Outlook.Application
  118.     Set messaggio = applic.CreateItem(olMailItem)
  119.     Dim indirizzi() As String
  120.     indirizzi = collectEmailAddresses()
  121.     For Each elemento In indirizzi
  122.         If (elemento <> "") Then
  123.             messaggio.Recipients.Add elemento
  124.         End If
  125.     Next
  126.     messaggio.Display
  127. End Sub
  128.  
  129.  
  130. Sub aggiungiBarraComandi()
  131. '---------------------------------------------------
  132. ' Esempio di come creare una nuova barra di comandi
  133. '---------------------------------------------------
  134. ' (c) 2003 Ivan Venuti e Marinella Lizza
  135. '---------------------------------------------------
  136.     Dim titolo As String
  137.     Dim cb As CommandBar
  138.     titolo = "Pulsantiera"
  139.     On Error Resume Next
  140.     Set cb = CommandBars.Item(titolo)
  141.     If cb Is Nothing Then
  142.         ' Non esiste: crea la barra dei comandi
  143.         Err.Clear
  144.         MsgBox "Creazione pulsantiera..."
  145.         Set cb = CommandBars.Add(titolo)
  146.         cb.Visible = True
  147.  
  148.         Dim cbc As CommandBarControl
  149.         Set cbc = cb.Controls.Add()
  150.         cbc.Style = msoButtonCaption
  151.         cbc.Caption = "[ Invia @ ]"
  152.         cbc.OnAction = "sendEmailAddresses"
  153.         cbc.TooltipText = "Prendi indirizzi email"
  154.  
  155.         Set cbc = cb.Controls.Add()
  156.         cbc.Style = Office.msoButtonCaption
  157.         cbc.Caption = "[ Links ]"
  158.         cbc.OnAction = "prendiLinks"
  159.         cbc.TooltipText = "Prendi link"
  160.  
  161.         Set cbc = cb.Controls.Add()
  162.         cbc.Style = Office.msoButtonCaption
  163.         cbc.Caption = "[ X ]"
  164.         cbc.OnAction = "elimina"
  165.         cbc.TooltipText = "Elimina pulsantiera"
  166.     Else
  167.         ' Esiste giα: la rende comunque visibile
  168.         cb.Visible = True
  169.     End If
  170. End Sub
  171.  
  172.  
  173. Sub aggiungiBarraComandiBis()
  174. '---------------------------------------------------
  175. ' Esempio di come creare una nuova barra di comandi
  176. '---------------------------------------------------
  177. ' (c) 2003 Ivan Venuti e Marinella Lizza
  178. '---------------------------------------------------
  179.     Dim titolo As String
  180.     Dim cb As CommandBar
  181.     titolo = "Pulsantiera"
  182.     On Error Resume Next
  183.     Set cb = CommandBars.Item(titolo)
  184.     If cb Is Nothing Then
  185.         ' Non esiste: crea la barra dei comandi
  186.         Err.Clear
  187.         MsgBox "Creazione pulsantiera..."
  188.         Set cb = CommandBars.Add(titolo)
  189.         cb.Visible = True
  190.         
  191.         ' Primo pulsante
  192.         Dim cbc As CommandBarControl
  193.         Set cbc = cb.Controls.Add()
  194.         cbc.Style = msoButtonCaption
  195.         cbc.Caption = "[ Invia @ ]"
  196.         cbc.OnAction = "sendEmailAddresses"
  197.         cbc.TooltipText = "Prendi indirizzi email"
  198.         
  199.         ' Secondo pulsante
  200.         Set cbc = cb.Controls.Add()
  201.         cbc.Style = msoButtonCaption
  202.         cbc.Caption = "[ Nuovo folder @ ]"
  203.         cbc.OnAction = "folderEmailAddresses"
  204.         cbc.TooltipText = "Prendi indirizzi email"
  205.         
  206.         ' Terzo pulsante
  207.         Set cbc = cb.Controls.Add()
  208.         cbc.Style = Office.msoButtonCaption
  209.         cbc.Caption = "[ Links ]"
  210.         cbc.OnAction = "prendiLinks"
  211.         cbc.TooltipText = "Prendi link"
  212.         
  213.         ' Quarto pulsante
  214.         Set cbc = cb.Controls.Add()
  215.         cbc.Style = Office.msoButtonCaption
  216.         cbc.Caption = "[ ? ]"
  217.         cbc.OnAction = "Help"
  218.         cbc.TooltipText = "Aiuto"
  219.         
  220.         ' Quinto pulsante
  221.         Set cbc = cb.Controls.Add()
  222.         cbc.Style = Office.msoButtonCaption
  223.         cbc.Caption = "[ X ]"
  224.         cbc.OnAction = "elimina"
  225.         cbc.TooltipText = "Elimina pulsantiera"
  226.     Else
  227.         ' Esiste giα: la rende comunque visibile
  228.         cb.Visible = True
  229.     End If
  230. End Sub
  231.  
  232. Sub elimina()
  233. '---------------------------------------------------
  234. ' Elimina la barra creata
  235. '---------------------------------------------------
  236. ' (c) 2003 Ivan Venuti e Marinella Lizza
  237. '---------------------------------------------------
  238.  
  239.     On Error Resume Next
  240.     Dim cb As CommandBar
  241.     titolo = "Pulsantiera"
  242.     Set cb = CommandBars(titolo)
  243.     cb.Delete
  244. End Sub
  245.  
  246. Sub Help()
  247. '---------------------------------------------------
  248. ' Aiuto sulle funzionalitα
  249. '---------------------------------------------------
  250. ' (c) 2003 Ivan Venuti e Marinella Lizza
  251. '---------------------------------------------------
  252.     MsgBox "Esempio di pulsantiera personalizzata"
  253. End Sub
  254.  
  255.  
  256. Sub applicaBubbleSort(StringArray() As String)
  257. '---------------------------------------------------
  258. ' IMPLEMENTAZIONE ALGORITMO DI ORDINAMENTO
  259. ' BUBBLE SORT
  260. '---------------------------------------------------
  261. ' (c) 2003 Ivan Venuti e Marinella Lizza
  262. '---------------------------------------------------
  263.   Dim a As String, b As String
  264.   Dim scambio As Boolean
  265.   Dim i As Integer
  266.   Dim ultimo As Integer
  267.   
  268.   ultimo = UBound(StringArray)
  269.   primo = LBound(StringArray)
  270.   scambio = True
  271.  
  272.   While scambio
  273.     scambio = False
  274.     For i = primo To ultimo - 1
  275.       a = StringArray(i)
  276.       b = StringArray(i + 1)
  277.       If a > b Then
  278.         StringArray(i) = b
  279.         StringArray(i + 1) = a
  280.         scambio = True
  281.       End If
  282.     Next
  283.     ultimo = ultimo - 1
  284.  Wend
  285. End Sub
  286.  
  287. Sub ordina()
  288. '---------------------------------------------------
  289. ' Esempio di applicazione algoritmo di ordinamento
  290. '---------------------------------------------------
  291. ' (c) 2003 Ivan Venuti e Marinella Lizza
  292. '---------------------------------------------------
  293.  Dim vettore(0 To 6) As String
  294.  vettore(0) = "Ivan"
  295.  vettore(1) = "Marinella"
  296.  vettore(2) = "Alan"
  297.  vettore(3) = "Roberto"
  298.  vettore(4) = "Francesco"
  299.  vettore(5) = "Ettore"
  300.  vettore(6) = "Alberto"
  301.  'applicaQuickSort vettore, 0, 6
  302.  applicaBubbleSort vettore
  303.  For i = 0 To 6
  304.     MsgBox i & "> " & vettore(i)
  305.  Next
  306. End Sub
  307.  
  308.  
  309. Sub applicaQuickSort(StringArray() As String, _
  310.         estremoSx As Long, estremoDx As Long)
  311. '---------------------------------------------------
  312. ' IMPLEMENTAZIONE ALGORITMO DI ORDINAMENTO
  313. ' QUICK SORT
  314. '---------------------------------------------------
  315. ' (c) 2003 Ivan Venuti e Marinella Lizza
  316. '---------------------------------------------------
  317.     Dim cursoreSx As Long
  318.     cursoreSx = estremoSx
  319.     Dim cursoreDx As Long
  320.     cursoreDx = estremoDx
  321.     Dim perno As String
  322.     perno = StringArray((estremoSx + estremoDx) \ 2)
  323.     While (cursoreSx <= cursoreDx)
  324.       While (StringArray(cursoreSx) < perno And cursoreSx < estremoDx)
  325.          cursoreSx = cursoreSx + 1
  326.       Wend
  327.       While (perno < StringArray(cursoreDx) And cursoreDx > estremoSx)
  328.          cursoreDx = cursoreDx - 1
  329.       Wend
  330.       If (cursoreSx <= cursoreDx) Then
  331.          tmpSwap = StringArray(cursoreSx)
  332.          StringArray(cursoreSx) = StringArray(cursoreDx)
  333.          StringArray(cursoreDx) = tmpSwap
  334.          cursoreSx = cursoreSx + 1
  335.          cursoreDx = cursoreDx - 1
  336.       End If
  337.     Wend
  338.     If (estremoSx < cursoreDx) Then
  339.         ' Applica quick sort sul sotto-vettore 1
  340.         applicaQuickSort StringArray, estremoSx, cursoreDx
  341.     End If
  342.     If (cursoreSx < estremoDx) Then
  343.         ' Applica quick sort sul sotto-vettore 2
  344.         applicaQuickSort StringArray, cursoreSx, estremoDx
  345.     End If
  346. End Sub
  347.  
  348.